﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class QuanLySanPhamController : Controller
    {
        //
        // GET: /QuanLySanPham/

        public ActionResult DanhSach()
        {
            ViewBag.MenuClass = Session["MenuClass"];
            DataClassesDataContext dataContext = new DataClassesDataContext();
            List<SanPhamModel> dsSanPhamModels = new List<SanPhamModel>();
            var dsSanPhamDiDongs = from sp in dataContext.SanPhams where sp.MucSp_Id == 1 select sp;
            foreach (SanPham sanPham in dsSanPhamDiDongs)
            {
                SanPhamModel sanPhamModel = new SanPhamModel();
                sanPhamModel.id = sanPham.Id;
                sanPhamModel.tenSanPham = sanPham.TenSp;
                sanPhamModel.giaSanPham = sanPham.GiaSp.Value;
                sanPhamModel.image = sanPham.Image;
                sanPhamModel.thongTinChiTiet = sanPham.ThongTinChiTiet;
                dsSanPhamModels.Add(sanPhamModel);
            }
            ViewBag.DanhSachDienThoaiDiDongs = dsSanPhamModels;
            return View();
        }

        public ActionResult DanhSachSanPhamTinHoc()
        {
            ViewBag.MenuClass = Session["MenuClass"];
            DataClassesDataContext dataContext = new DataClassesDataContext();
            List<SanPhamModel> dsSanPhamModels = new List<SanPhamModel>();
            var dsSanPhamTinHocs = from sp in dataContext.SanPhams where sp.MucSp_Id == 2 select sp;
            foreach (SanPham sanPham in dsSanPhamTinHocs)
            {
                SanPhamModel sanPhamModel = new SanPhamModel();
                sanPhamModel.id = sanPham.Id;
                sanPhamModel.tenSanPham = sanPham.TenSp;
                sanPhamModel.giaSanPham = sanPham.GiaSp.Value;
                sanPhamModel.image = sanPham.Image;
                sanPhamModel.thongTinChiTiet = sanPham.ThongTinChiTiet;
                dsSanPhamModels.Add(sanPhamModel);
            }
            ViewBag.DanhSachSanPhamTinHocs = dsSanPhamModels;
            return View();
        }

        public ActionResult ThemMoi()
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            var dsLoaiSanPham = from loaiSanPham in dataContext.MucSanPhams select loaiSanPham;
            ViewBag.DsLoaiSanPham = new SelectList(dsLoaiSanPham, "Id", "TenMucSp");
            ViewBag.MenuClass = Session["MenuClass"];
            return View();
        }

        [HttpPost]
        public ActionResult ThemMoi(SanPhamModel model, int MucSpId)
        {
            SanPham sanPham = new SanPham();
            sanPham.MucSp_Id = MucSpId;
            sanPham.TenSp = model.tenSanPham;
            sanPham.GiaSp = model.giaSanPham;
            sanPham.Image = model.image;
            sanPham.ThongTinChiTiet = model.thongTinChiTiet;
            DataClassesDataContext dataContext = new DataClassesDataContext();
            dataContext.SanPhams.InsertOnSubmit(sanPham);
            dataContext.SubmitChanges();
            return RedirectToAction("DanhSach");
        }

    }
}
